//
//  LSChatPhotoView.m
//  LookScore
//
//  Created by wws on 2018/3/26.
//  Copyright © 2018年 ChongQingXunTiCompany. All rights reserved.
//

#import "LSChatPhotoView.h"
#import <UIView+SDAutoLayout.h>
#import "SDPhotoBrowser.h"
@interface LSChatPhotoView ()<SDPhotoBrowserDelegate>

@property (nonatomic, strong) NSArray *imageViewsArray;

@end

@implementation LSChatPhotoView

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self setSubViews];
    }
    
    return self;
}


- (void)setSubViews{
    NSMutableArray *temp = [NSMutableArray new];
    for (int i = 0; i < 9; i++) {
        UIImageView *imageView = [UIImageView new];
        [self addSubview:imageView];
        imageView.userInteractionEnabled = YES;
        imageView.tag = i;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImageView:)];
        [imageView addGestureRecognizer:tap];
        [temp addObject:imageView];
    }
    
    self.imageViewsArray = [temp copy];
}

- (void)setPicPathStringsArray:(NSArray *)picPathStringsArray{
    _picPathStringsArray = picPathStringsArray;
    for (long i = _picPathStringsArray.count; i < self.imageViewsArray.count; i++) {
        UIImageView *imageView = [self.imageViewsArray objectAtIndex:i];
        imageView.hidden = YES;
    }
    
    if (_picPathStringsArray.count == 0) {
        self.height = 0;
        self.fixedHeight = @(0);
        return;
    }
    
    CGFloat itemW = [self itemWidthForPicPathArray:_picPathStringsArray];
    CGFloat  itemH = 0;
    if (_picPathStringsArray.count == 1) {

        UIImageView * imageView = [[UIImageView alloc] init];
        [imageView sd_setImageWithURL:[NSURL URLWithString:_picPathStringsArray.firstObject] placeholderImage:nil];
    
//        if (imageView.image.size.width) {
//
//            itemH = imageView.image.size.height / imageView.image.size.width * itemW;
//        }else{
//
//
//        }
        itemH = itemW * 9 / 16;
    } else {
        itemH = itemW * 9 / 16;
    }
    long perRowItemCount = [self perRowItemCountForPicPathArray:_picPathStringsArray];
    CGFloat margin = 10;
    
    [_picPathStringsArray enumerateObjectsUsingBlock:^(NSString *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        long columnIndex = idx % perRowItemCount;
        long rowIndex = idx / perRowItemCount;
        UIImageView *imageView = [_imageViewsArray objectAtIndex:idx];
        imageView.hidden = NO;
        [imageView sd_setImageWithURL:[NSURL URLWithString:obj] placeholderImage:nil];
        if (idx<=2) {
            imageView.frame = CGRectMake(columnIndex * (itemW + margin), rowIndex * (itemH + margin), itemW, itemH);
        }
        
        if (picPathStringsArray.count > 3) {
            if (idx == 2) {
                
                UILabel * label = [[UILabel alloc] init];
                label.text = [NSString stringWithFormat:@"%ld",_picPathStringsArray.count];
                label.textColor = LSTitleWhiteColor;
                label.textAlignment = NSTextAlignmentCenter;
                label.font = LSFontSize12;
                label.backgroundColor = Color(255, 158, 68);
                label.frame = CGRectMake(imageView.frame.size.width - 25, imageView.frame.size.height-15, 25, 15);
                //切1/4圆角
                UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:label.bounds byRoundingCorners:UIRectCornerTopLeft  cornerRadii:CGSizeMake(10, 10)];
                CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
                maskLayer.frame = label.bounds;
                maskLayer.path = maskPath.CGPath;
                label.layer.mask = maskLayer;
                [imageView addSubview:label];
            }
        }
    }];
    
    CGFloat w = perRowItemCount * itemW + (perRowItemCount - 1) * margin;
//    int columnCount = ceilf(_picPathStringsArray.count * 1.0 / perRowItemCount);
//    CGFloat h = columnCount * itemH + (columnCount - 1) * margin;
    CGFloat h = 1 * itemH + (1-1)* margin;
    self.width = w;
    self.height = h;
    
    self.fixedHeight = @(h);
    self.fixedWidth = @(w);
}
- (CGFloat)itemWidthForPicPathArray:(NSArray *)array{
//    if (array.count == 1) {
//        return 120;
//    } else {
//        return (LSScreenWidth - 50) / 3;
//    }
    return (LSScreenWidth - 40) / 3;
}

- (NSInteger)perRowItemCountForPicPathArray:(NSArray *)array{
    if (array.count < 3) {
        return array.count;
    }
    
//    else if (array.count <= 4) {
//        return 2;
//    }
    
    else {
        return 3;
    }
}

- (void)tapImageView:(UITapGestureRecognizer *)tap{
    UIView *imageView = tap.view;
    SDPhotoBrowser *browser = [[SDPhotoBrowser alloc] init];
    browser.currentImageIndex = imageView.tag;
    browser.sourceImagesContainerView = self;
    browser.imageCount = self.picPathStringsArray.count;
    browser.delegate = self;
    [browser show];
}
#pragma mark - SDPhotoBrowserDelegate

- (NSURL *)photoBrowser:(SDPhotoBrowser *)browser highQualityImageURLForIndex:(NSInteger)index{
    NSString *imageName = self.picPathStringsArray[index];
    NSURL *url = [[NSBundle mainBundle] URLForResource:imageName withExtension:nil];
    return url;
}

- (UIImage *)photoBrowser:(SDPhotoBrowser *)browser placeholderImageForIndex:(NSInteger)index{
    UIImageView *imageView = self.subviews[index];
    return imageView.image;
}


@end
